home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / mach / hurd / stdio_init.c < prev    next >
C/C++ Source or Header  |  1994-05-23  |  3KB  |  83 lines

  1. /* Copyright (C) 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. #include <ansidecl.h>
  20. #include <stdio.h>
  21. #include <sys/types.h>
  22. #include <sys/stat.h>
  23. #include <unistd.h>
  24. #include <hurd/fd.h>
  25.  
  26. /* Initialize STREAM as necessary.
  27.    This may change I/O functions, give a buffer, etc.
  28.    If no buffer is allocated, but the bufsize is set,
  29.    the bufsize will be used to allocate the buffer.  */
  30. void
  31. DEFUN(__stdio_init_stream, (stream), FILE *stream)
  32. {
  33.   struct hurd_fd *const d = stream->__cookie;
  34.   struct stat statb;
  35.   error_t err;
  36.  
  37.   if (stream->__buffer != NULL || stream->__userbuf)
  38.     /* If's unbuffered by request, we can't do anything useful.  */
  39.     return;
  40.  
  41.   /* Find out what sort of file this is.  */
  42.   __spin_lock (&d->port.lock);
  43.   if (err = HURD_FD_PORT_USE (d, __io_stat (port, &statb)))
  44.     return;
  45.  
  46.   if (S_ISFIFO (statb.st_mode))
  47.     {
  48.       /* It's a named pipe (FIFO).  Make it unbuffered.  */
  49.       stream->__userbuf = 1;
  50.       return;
  51.     }
  52.  
  53.   if (S_ISCHR (statb.st_mode))
  54.     {
  55.       /* It's a character device.
  56.      Make it line-buffered if it's a terminal.  */
  57.       mach_port_t cttyid;
  58.       __spin_lock (&d->port.lock);
  59.       if (! HURD_FD_PORT_USE (d, __term_getctty (port, &cttyid)))
  60.     {
  61.       __mach_port_deallocate (__mach_task_self (), cttyid);
  62.  
  63.       stream->__linebuf = 1;
  64.  
  65.       /* Unix terminal devices have the bad habit of claiming to be
  66.          seekable.  On systems I have tried, seeking on a terminal
  67.          device seems to set its file position as specified, such that
  68.          a later tell says the same thing.  This is in no way related
  69.          to actual seekability--the ability to seek back and read old
  70.          data.  Unix terminal devices will let you "seek back", and
  71.          then read more new data from the terminal.  I can think of
  72.          nothing to do about this lossage except to preemptively disable
  73.          seeking on terminal devices.  */
  74.  
  75.       stream->__io_funcs.__seek = NULL; /* Seeks get ESPIPE.  */
  76.     }
  77.     }
  78.   
  79.   /* Use the block-size field to determine
  80.      the system's optimal buffering size.  */
  81.   stream->__bufsize = statb.st_blksize;
  82. }
  83.